const {comMysql} = require("../db/mysql")
const md5 = require("md5")
const gravatar = require("gravatar")
const xss = require("xss")
const fs = require("fs")


const Login = (req,res)=>{
  let {username,password} = req.body
  let salt="zxh";
  password=md5(password+salt).substring(0,10)
  let sql = `select * from users where username = ? and password = ?`
  let arr = [username,password]
  return comMysql(sql,arr)
}

const Avatar = (req,res)=>{
  let {avatar,username} = req.query
  avatar = "http://localhost:3001/api/upload/showImg?avatar="+avatar+"&username="+username
  let sql = "select * from users where username = ? and avatar = ?"
  let arr = [username,avatar]
  return comMysql(sql,arr)
}

const search = (req,res)=>{
    let sql = "select * from users"
    return comMysql(sql,arr)
}
const searchFilter = (req,res)=>{
    let {username} = req.query
    let sql = `select * from users where username like "%${username}%"`
    return comMysql(sql)
}
const same = (req,res)=>{
let {username} = req.body

  let sql = `select * from users where username = ?`
let arr = [username]
return comMysql(sql,arr)
}

const Register = (req,res)=>{  
let {username,password,realname} = req.body
let salt="zxh";
password=md5(password+salt).substring(0,10)
//let avatar = gravatar.url(username, {s: '200', r: 'pg', d: 'mm'});
let avatar = 'http://localhost:3001/api/avatar'
let time = new Date().getTime()
let status = 1
let sql = `insert into users (username,password,status,avatar,time,realname) values (?,?,?,?,?,?)`
arr = [username,password,status,avatar,time,realname]
return comMysql(sql,arr)
}
const delUser = (req,res)=>{
    let {username} = req.body
    let sql = "delete from users where username = ?"
    let arr = [username]
    return comMysql(sql,arr)
}
const searchAvatarForDel = (req,res)=>{
  let {username} = req.body
  let sql = "select * from users where username = ?"
  let arr = [username]
  return comMysql(sql,arr)
}
const blogManager = async (req,res)=>{
    let sql = "select realname,avatar from users where username = 2628735401"
    return comMysql(sql)
}

const update =async (req,res)=>{
  let {username,realname,status} = req.body
  let moreUpdate= ` `
  realname=xss(realname)
  // console.log(req.body.avatar+"zzxcz");
  let avatar = req.body.avatar
  
  let arr = []
  if(avatar){
    moreUpdate=moreUpdate+`avatar = ? ,`
    arr.push(avatar)
  }
  if(realname){
    moreUpdate=moreUpdate+`realname = ? ,`
    arr.push(realname)
    
  }
  if(status){
      moreUpdate=moreUpdate+`status = ? ,`
    arr.push(status)
  }
  moreUpdate = moreUpdate.substring(0,moreUpdate.length-1)
  arr.push(username)
  let data =await searchAvatarForDel(req,res)
    if(data.length>0&&data[0].avatar){
      let num = data[0].avatar.indexOf("?")      
      let next1=data[0].avatar.substring(num)
      let next2=next1.split("&")[0].split("=")[1]
      let next3 = "./"+next2
      if(fs.existsSync(next3)){
        fs.unlink(next3,(err)=>{
          if(!err){
            console.log("删除成功");
          }else{
            console.log("删除失败");
            
          }
        })
      }
    }

  let sql = `update users set`+moreUpdate+` where username = ?`
  return comMysql(sql,arr)
}
module.exports={
  Login,
  Register,
  same,
  update,
  Avatar,
  search,
  searchFilter,
  delUser,
  blogManager,
}